/**
 * Created by 都大爽 on 2017/8/7.
 */
import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

const debug = process.env.NODE_ENV !== 'production';

export default new Vuex.Store({
    state: {
        /**
         * 页面标题
         */
        title: '',
        /**
         * 用户信息
         */
        user: {
            name: '',
            email: '',
            mobile: '',
            permissions: [], //权限列表
            can: function (permission) {
                return _.indexOf(this.permissions, permission) > -1;
            }
        },
        /**
         * 面包屑
         */
        breadCrumbs: [],

        /**
         * 省份列表
         */
        provinceList: [],

        /**
         * 职称列表
         */
        positionTitleList: []
    },
    getters: {
        getProvinceListLength: state => {
            return state.provinceList.length;
        },
        getPositionTitleListLength: state => {
            return state.positionTitleList.length;
        }
    },
    mutations: {
        setTitle (state, title) {
            // 变更状态
            state.title = title;
            document.title = '学院奖后台-' + state.title;
        },
        setUser(state, user){
            debug && console.log(user);
            state.user.name = user.name;
            state.user.email = user.email;
            state.user.mobile = user.profile_info.mobile;
        },
        setUserPermissions(state, permissions){
            debug && console.log(permissions);
            state.user.permissions = permissions;
        },
        setBreadCrumbs(state, breadCrumbs){
            debug && console.log(breadCrumbs);
            state.breadCrumbs = breadCrumbs;
        },
        setProvinceList(state, provinceList){
            debug && console.log(provinceList);
            state.provinceList = provinceList;
        },
        setPositionalTitleList(state, positionTitleList){
            debug && console.log(positionTitleList);
            state.positionTitleList = positionTitleList;
        }
    },
    actions: {
        fetchUser({commit}){
            axios.get('api/admin/profile').then(function (response) {
                debug && console.log('Susscee:', response);
                commit('setUser', response.data);
            }).catch(function (error) {
                // Something happened in setting up the request that triggered an Error
                debug && console.log('Error', error.message);
            });
        },
        fetchUserPermissions({commit}){
            axios.get('api/admin/permission').then(function (response) {
                debug && console.log('Susscee:', response);
                commit('setUserPermissions', response.data);
            }).catch(function (error) {
                // Something happened in setting up the request that triggered an Error
                debug && console.log('Error', error.message);
            });
        },
        fetchProvinceList({commit}){
            axios.get('api/data/province-list').then(function (response) {
                debug && console.log('Susscee:', response);
                commit('setProvinceList', response.data);
            }).catch(function (error) {
                // Something happened in setting up the request that triggered an Error
                debug && console.log('Error', error.message);
            });
        },
        fetchPositionalTitleList({commit}){
            axios.get('api/data/position-title-list').then(function (response) {
                debug && console.log('Susscee:', response);
                commit('setPositionalTitleList', response.data);
            }).catch(function (error) {
                // Something happened in setting up the request that triggered an Error
                debug && console.log('Error', error.message);
            });
        }
    },
    modules: {
        // member
    },

    strict: debug,
    // plugins: debug ? [createLogger()] : []
})
